var express = require("express");
var router = express.Router();
var mysql = require("mysql");
var DateUtil = require('../lib/utils/DateUtil');
var _ = require("underscore");

var connection = require('../lib/utils/DbconnectUtil');

/**
 * 查询文章列表
 */
/*
router.get('/', function (req, res, next) {
    connection().query(querySql, function (error, rows) {
        if (error) {
            console.log(error);
            next(error);
        } else {
            res.send(_.map(rows, function (data) {
                return _.extend(data, {
                    create_at: DateUtil.dateFormat(data.create_at, 'yyyy-MM-dd hh:mm'),
                    update_at: DateUtil.dateFormat(data.update_at, 'yyyy-MM-dd hh:mm')
                });
            }));
        }
    });
});
*/

router.get('/:submenuId/:contentId', function (req, res, next) {
    var contentId = req.params.contentId;
    var queryContentSql = 'select ' +
        't_content.id id, ' +
        't_content.name name, ' +
        't_content.summary summary, ' +
        't_content.type type, ' +
        't_content.coverUrl coverUrl, ' +
        't_content.videoUrl videoUrl, ' +
        't_content.updateAt updateAt, ' +
        't_content.stick stick, ' +
        't_content.content content, ' +
        't_content.menu subMenuId ' +
        'from t_content ' +
        'where t_content.id = ? ';

    // console.log(queryContentSql);
    connection().query(queryContentSql, [contentId], function (error, results) {
        if (error || results.length == 0) {
            console.log(error);
            res.send({code: -1, message: '查询数据失败'});
        } else {
            res.send({code: 0, data: results[0]});
        }
    });
});

router.get('/:subMenuId/', function (req, res, next) {
    var subMenuId = req.params.subMenuId;
    var skip = req.query.skip;
    var limit = req.query.limit;
    var singleData = req.query.singleData;
    var queryContentSql = "";

    //要求只返回一条数据详情
    if(singleData === 'Y'){
        skip = 0 ;
        limit = 1;
        queryContentSql = 'select ' +
            't_content.id id, ' +
            't_content.name name, ' +
            't_content.summary summary, ' +
            't_content.type type, ' +
            't_content.coverUrl coverUrl, ' +
            't_content.videoUrl videoUrl, ' +
            't_content.updateAt updateAt, ' +
            't_content.stick stick, ' +
            't_content.content content, ' +
            't_content.menu subMenuId ' +
            'from t_content ' +
            'where t_content.menu = ? order by t_content.updateAt desc limit ?,?';
    }
    else{

        if (skip) {
            skip = parseInt(skip);
        }
        else {
            skip = 0;
        }

        if (limit) {
            limit = parseInt(limit);
        }
        else {
            limit = 5;
        }

        queryContentSql = 'select ' +
            't_content.id id, ' +
            't_content.name name, ' +
            't_content.summary summary, ' +
            't_content.type type, ' +
            't_content.coverUrl coverUrl, ' +
            't_content.videoUrl videoUrl, ' +
            't_content.updateAt updateAt, ' +
            't_content.stick stick ' +
            ' from t_content ' +
            ' where t_content.menu = ? order by t_content.updateAt desc limit ?,?';
    }

    connection().query(queryContentSql, [subMenuId, skip, limit], function (error, results) {
        if (error) {
            console.log(error);
            res.send({code: -1, message: '查询数据失败'});
        } else {
            if (singleData === 'Y') {
                res.send({code: 0, data: results[0]});
            }
            else {
                res.send({code: 0, data: results});
            }

        }
    });
});

module.exports = router;